cmake_minimum_required(VERSION 3.16)

project(Greeter C CXX)


set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}/bin)


include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/common.cmake)

# Proto file
get_filename_component(proto "${CMAKE_CURRENT_SOURCE_DIR}/proto/greeter.proto" ABSOLUTE)
get_filename_component(proto_path "${proto}" PATH)

# Generated sources
set(proto_hdrs "${CMAKE_CURRENT_SOURCE_DIR}/proto/greeter.pb.h")
set(proto_srcs "${CMAKE_CURRENT_SOURCE_DIR}/proto/greeter.pb.cc")
set(grpc_hdrs  "${CMAKE_CURRENT_SOURCE_DIR}/proto/greeter.grpc.pb.h")
set(grpc_srcs  "${CMAKE_CURRENT_SOURCE_DIR}/proto/greeter.grpc.pb.cc")


add_custom_command(
      OUTPUT "${proto_hdrs}" "${proto_srcs}" "${grpc_hdrs}" "${grpc_srcs}"
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out "${CMAKE_CURRENT_SOURCE_DIR}/proto"
           --cpp_out "${CMAKE_CURRENT_SOURCE_DIR}/proto"
          -I "${proto_path}"
          --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
          "${proto}"
      DEPENDS "${proto}")


  
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/proto")


add_library(grpc_proto
          ${grpc_srcs}
          ${grpc_hdrs}
          ${proto_srcs}
          ${proto_hdrs}
        )


target_link_libraries(grpc_proto
                    ${_REFLECTION}
                    ${_GRPC_GRPCPP}
                    ${_PROTOBUF_LIBPROTOBUF}
                  )

foreach(_target client server)

  add_executable(${_target} "cpp/${_target}.cpp")

  target_link_libraries(${_target}
                      grpc_proto
                    )
endforeach()